home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / samples / blendxor.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-15  |  3.6 KB  |  185 lines

  1. /*
  2. ** blendxor.c - Demonstrates the use of the blend_logic_op
  3. **    extension to draw hilights.  Using XOR to draw the same
  4. **    image twice restores the background to its original value.
  5. */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9. #ifndef _WIN32
  10. #include <unistd.h>
  11. #endif
  12. #include <stdlib.h>
  13. #include <GL/glut.h>
  14.  
  15. #ifdef AMIGA
  16. #include <clib/dos_protos.h>
  17. #ifndef __PPC__
  18. #include <pragma/dos_lib.h>
  19. #endif
  20. #endif
  21.  
  22.  
  23. GLenum doubleBuffer;
  24. int dithering = 0;
  25. GLint windW, windH;
  26.  
  27. static void Init(void)
  28. {
  29.     glDisable(GL_DITHER);
  30.     glShadeModel(GL_FLAT);
  31. }
  32.  
  33. static void Reshape(int width, int height)
  34. {
  35.  
  36.     windW = (GLint)width;
  37.     windH = (GLint)height;
  38.  
  39.     glViewport(0, 0, (GLint)width, (GLint)height);
  40.  
  41.     glMatrixMode(GL_PROJECTION);
  42.     glLoadIdentity();
  43.     gluOrtho2D(0, 400, 0, 400);
  44.     glMatrixMode(GL_MODELVIEW);
  45. }
  46.  
  47. static void Key(unsigned char key, int x, int y)
  48. {
  49.  
  50.     switch (key) {
  51.       case 27:
  52.         exit(1);
  53.       case 'd':
  54.         dithering = !dithering;
  55.         break;
  56.       default:
  57.         return;
  58.     }
  59.  
  60.     glutPostRedisplay();
  61. }
  62.  
  63. static void Draw(void)
  64. {
  65.     int i;
  66.  
  67.     glDisable(GL_BLEND);
  68.  
  69.     (dithering) ? glEnable(GL_DITHER) : glDisable(GL_DITHER);
  70.  
  71.     glClearColor(0.5, 0.6, 0.1, 1.0);
  72.     glClear(GL_COLOR_BUFFER_BIT);
  73.  
  74.     /* Draw background prims */
  75.     glColor3f(0.1, 0.1, 1.0);
  76.     glBegin(GL_TRIANGLES);
  77.         glVertex2i(5, 5);
  78.         glVertex2i(130, 50);
  79.         glVertex2i(100,  300);
  80.     glEnd();
  81.     glColor3f(0.5, 0.2, 0.9);
  82.     glBegin(GL_TRIANGLES);
  83.         glVertex2i(200, 100);
  84.         glVertex2i(330, 50);
  85.         glVertex2i(340,  400);
  86.     glEnd();
  87.  
  88.     glEnable(GL_BLEND);
  89.     glBlendEquationEXT(GL_LOGIC_OP);
  90.     glLogicOp(GL_XOR);
  91.  
  92.     /* Draw a set of rectangles across the window */
  93.     glColor3f(0.9, 0.2, 0.8);
  94.     for(i = 0; i < 400; i+=60) {
  95.         glBegin(GL_POLYGON);
  96.             glVertex2i(i, 100);
  97.             glVertex2i(i+50, 100);
  98.             glVertex2i(i+50, 200);
  99.             glVertex2i(i, 200);
  100.         glEnd();
  101.     }
  102.     glFlush();   /* Added by Brian Paul */
  103. #ifndef _WIN32
  104. #ifndef AMIGA
  105.     sleep(2);
  106. #else
  107.     Delay(100);
  108. #endif
  109. #endif
  110.  
  111.     /* Redraw  the rectangles, which should erase them */
  112.     for(i = 0; i < 400; i+=60) {
  113.         glBegin(GL_POLYGON);
  114.             glVertex2i(i, 100);
  115.             glVertex2i(i+50, 100);
  116.             glVertex2i(i+50, 200);
  117.             glVertex2i(i, 200);
  118.         glEnd();
  119.     }
  120.     glFlush();
  121.  
  122.  
  123.     if (doubleBuffer) {
  124.         glutSwapBuffers();
  125.     }
  126. }
  127.  
  128. static GLenum Args(int argc, char **argv)
  129. {
  130.     GLint i;
  131.  
  132.     doubleBuffer = GL_FALSE;
  133.  
  134.     for (i = 1; i < argc; i++) {
  135.         if (strcmp(argv[i], "-sb") == 0) {
  136.             doubleBuffer = GL_FALSE;
  137.         } else if (strcmp(argv[i], "-db") == 0) {
  138.             doubleBuffer = GL_TRUE;
  139.         } else {
  140.             printf("%s (Bad option).\n", argv[i]);
  141.             return GL_FALSE;
  142.         }
  143.     }
  144.     return GL_TRUE;
  145. }
  146.  
  147. void main(int argc, char **argv)
  148. {
  149.     GLenum type;
  150.     char *s;
  151.     char *extName = "GL_EXT_blend_logic_op";
  152.  
  153.     glutInit(&argc, argv);
  154.  
  155.     if (Args(argc, argv) == GL_FALSE) {
  156.         exit(1);
  157.     }
  158.  
  159.     glutInitWindowPosition(0, 0); glutInitWindowSize( 400, 400);
  160.  
  161.     type = GLUT_RGB;
  162.     type |= (doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE;
  163.     glutInitDisplayMode(type);
  164.  
  165.     if (glutCreateWindow("Blend XOR") == GL_FALSE) {
  166.         exit(1);
  167.     }
  168.  
  169.     /* Make sure blend_logic_op extension is there. */
  170.     s = (char *) glGetString(GL_EXTENSIONS);
  171.     if (!s)
  172.         exit(1);
  173.     if (strstr(s,extName) == 0) {
  174.         printf("Blend_logic_op extension is not present.\n");
  175.         exit(1);
  176.     }
  177.  
  178.     Init();
  179.  
  180.     glutReshapeFunc(Reshape);
  181.     glutKeyboardFunc(Key);
  182.     glutDisplayFunc(Draw);
  183.     glutMainLoop();
  184. }
  185.